//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.orphan.supplier.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orphan.supplier.mapper.TempTenderCallToGoodsMapper;
import com.orphan.supplier.mapper.TenderCallMapper;
import com.orphan.supplier.po.Goods;
import com.orphan.supplier.po.TempTenderCallToGoods;
import com.orphan.supplier.po.Tender;
import com.orphan.supplier.po.TenderCall;
import com.orphan.supplier.service.IGoodsService;
import com.orphan.supplier.service.ITenderCallService;
import com.orphan.supplier.service.ITenderService;
import com.orphan.supplier.vo.TenderCallVO;
import com.orphan.util.dto.PurchaseGoodDto;
import com.orphan.util.dto.PurchaseOrderDto;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;

import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

@Service("iTenderCallService")
public class TenderCallServiceImpl extends ServiceImpl<TenderCallMapper, TenderCall> implements ITenderCallService {
    @Resource
    private TenderCallMapper mapper;
    @Resource
    private IGoodsService goodsService;
    @Resource
    private TempTenderCallToGoodsMapper tempTenderCallToGoodsMapper;
    @Resource
    private ITenderService tenderService;

    public TenderCallServiceImpl() {
    }


    public ArrayList<TenderCall> list(Integer pageNo,Integer pageSize) {
        int[] integers = pageManager(pageNo, pageSize);
        ArrayList<TenderCall> tenderCalls = makeTenderCallList(this.mapper.findTenderCalls(integers[0],integers[1]));
        return tenderCalls;
    }

    public ArrayList<TenderCall> list() {
        return makeTenderCallList(mapper.findAll());
    }

    private ArrayList<TenderCall> makeTenderCallList(ArrayList<TenderCall> tenderCalls) {
        ArrayList<TempTenderCallToGoods> tempTenderCallToGoods = this.tempTenderCallToGoodsMapper.findAll();
        for (int i = 0; i < tenderCalls.size(); i++) {
            List<Goods> goodsList = new ArrayList<>();
            TenderCall tenderCall = tenderCalls.get(i);
            for (int j = 0; j < tempTenderCallToGoods.size(); j++) {
                TempTenderCallToGoods tempTenderCallToGoods1 = tempTenderCallToGoods.get(j);
                if (tenderCall.getNumber().equals(tempTenderCallToGoods1.getCallNumber())) {
                    Goods goods = getGoods(tempTenderCallToGoods1.getGoodsNumber());
                    goods.setQuantity(tempTenderCallToGoods1.getCallAmount());
                    goodsList.add(goods);
                    System.out.println("goods = " + goods);
                }
            }
            tenderCalls.get(i).setGoods(goodsList);
        }
        return tenderCalls;
    }

    private Goods getGoods(Integer goodsNumber) {
        ArrayList<Goods> list = this.goodsService.list();
        for (Goods g:list) {
            if (g.getId() == goodsNumber) {
                return g;
            }
        }
        return null;
    }

    public void add(PurchaseOrderDto purchaseOrderDto) throws ParseException {
        TenderCall tenderCall = new TenderCall();
        tenderCall.setNumber(purchaseOrderDto.getOrderNum());
        List<Goods> list = new ArrayList();

        for(int i = 0; i < purchaseOrderDto.getGoods().size(); ++i) {
            list.add(new Goods((PurchaseGoodDto)purchaseOrderDto.getGoods().get(i)));
        }

        tenderCall.setGoods(list);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        tenderCall.setStart(sdf.parse(purchaseOrderDto.getOrderTime()));
        tenderCall.setEnd(sdf.parse(purchaseOrderDto.getEndTime()));
        tenderCall.setTitle(purchaseOrderDto.getTitle());
        tenderCall.setContent(purchaseOrderDto.getContent());
        this.mapper.add(tenderCall);
    }

    private int[] pageManager(int pageNo,int pageSize) {
        int[] result = new int[2];
        result[0] = pageNo * pageSize - pageSize;
        result[1] = pageSize;
        return result;
    }

    public List<TenderCallVO> listByUsername(String username, Integer pageNo,Integer pageSize) {
        ArrayList<TenderCall> list = this.list(pageNo,pageSize);
        List<Tender> tenders = this.tenderService.thisUser(username);
        List<TenderCallVO> result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            TenderCall tenderCall = list.get(i);
            TenderCallVO tenderCallVO = new TenderCallVO(tenderCall);
            List<Tender> tendersList = new ArrayList<>();
            for (int j = 0; j < tenders.size(); j++) {
                Tender tender = tenders.get(j);
                if (tender.getCall().getNumber().equals(tenderCallVO.getTenderCallNumber())) {
                    tendersList.add(tender);
                }
            }
            for (Tender t:tendersList) {
                System.out.println("t = " + t);
            }
            tenderCallVO.setTenderStatus(tendersList);
            result.add(tenderCallVO);
        }
//        System.out.println("=================TenderCallServiceImpl.listByUsername=======================");
//        for (TenderCallVO t:result) {
//            System.out.println("t = " + t);
//        }
//        System.out.println("=================TenderCallServiceImpl.listByUsername=======================");
        return result;
    }
}
